package com.Babysleep.manage;

import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.Babysleep.R;
import com.Babysleep.Activity.BasicActivity;
import com.Babysleep.Interface.ListenHandler;

/**
 * 播放单曲类
 * 
 */
public class DanQuPlay extends Listen {

	MyHandler handler;
	TextView daojishi;

	public DanQuPlay(BasicActivity activity) {
		super(activity);
		daojishi = (TextView) activity.findViewById(R.id.daojishi);
		handler = new MyHandler(Constant.delay_length);
	}

	@Override
	public void Play() {
		// TODO Auto-generated method stub
		if (playing_started == false) {
			System.out.println("laile");
			handler.start();
		}
		super.Play();
	}

	@Override
	public void Pause() {
		// TODO Auto-generated method stub
		super.Pause();// 播放控制
		handler.pause();// handler控制
	}

	@Override
	public void Restart() {
		// TODO Auto-generated method stub
		super.Restart();
		handler.restart();
	}

	@Override
	public void Stop() {
		// TODO Auto-generated method stub
		super.Stop();
		handler.stop();
	}

	public class MyHandler extends ListenHandler {

		public MyHandler(int delay) {
			super(delay);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				if (need_send == true) {
					// UI();
					if (activity.cycle_length != 0)
						count();
					else if (daojishi.getVisibility() == View.VISIBLE)
						daojishi.setText(df.format(0));
					sendEmptyMessageDelayed(1, delay);
				}
				break;

			default:
				break;
			}
		}

	}

	public void count() {
		super.count();
		System.out.println(counter);
		if (daojishi.getVisibility() == View.VISIBLE) {
			((TextView) activity.findViewById(R.id.daojishi)).setText(df
					.format(counter));
			if (counter <= 0) {
				((TextView) activity.findViewById(R.id.daojishi)).setText(df
						.format(0));
				Stop();
			}
		} else if (counter <= 0)
			Stop();
	}

}
